package com.kokuma.drawsprite.app.web;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.kokuma.drawsprite.common.NormalReturnFailureBean;
import com.kokuma.drawsprite.common.NormalReturnSuccessBean;
import com.kokuma.drawsprite.common.NormalReturnValue;
import com.kokuma.drawsprite.common.StaticValue;
import com.kokuma.drawsprite.ftp.models.CfgFtpPath;
import com.kokuma.drawsprite.ftp.services.interfaces.ICfgFtpPathServices;
import com.kokuma.drawsprite.ftp.util.FTPUtil;
import com.kokuma.drawsprite.guide.services.interfaces.IBsUploadFileInfoServices;

@Controller
@RequestMapping("appController")
public class AppController {
	private static Logger logger = LoggerFactory.getLogger(AppController.class);

	@Resource
	ICfgFtpPathServices cfgFtpPathServices;

	@Resource
	IBsUploadFileInfoServices bsUploadFileInfoServices;

	@RequestMapping(value = "/test", method = { RequestMethod.POST, RequestMethod.GET })
	public void test(HttpServletRequest req, HttpServletResponse res) throws Exception {
		// Map map = guideServices.getGuideUploadInfo();
		res.getWriter().write("helloworld");
	}

	@RequestMapping(value = "/jsonTest", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody CfgFtpPath jsonTest(HttpServletRequest req, HttpServletResponse res) throws Exception {
		List<CfgFtpPath> list = cfgFtpPathServices.getCfgFtpPath(StaticValue.CFG_FTP_CODE_GUIDE);
		return list.get(0);
	}

	@RequestMapping(value = "/uploadGuide", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody NormalReturnValue uploadGuide(@RequestParam("file") MultipartFile file, HttpServletRequest req,
			HttpServletResponse res) {
		try {
			JSONObject ret = bsUploadFileInfoServices.upUploadGuideFile(file, null);
			NormalReturnSuccessBean succ= new NormalReturnSuccessBean();
			succ.getParams().setInfo(ret);
			return succ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("上传guide文件失败:", e);
			return new NormalReturnFailureBean(e.getMessage());
		}
	}
}